package com.cy2;

/**
 * @author CY
 * @date 2022/08/13 15:28
 **/
class CloneTest implements Cloneable {
    int age = 21;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Comparable<Student>, Cloneable {
    private String name;
    private int age;
    CloneTest cloneTest;

    public Student(String name, int age, CloneTest cloneTest) {
        this.name = name;
        this.age = age;
        this.cloneTest = cloneTest;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
//        CloneTest tmp=(CloneTest)cloneTest.clone();
//        return new Student(name,age,tmp);
        Student tmp1 = (Student) super.clone();
        tmp1.cloneTest = (CloneTest) this.cloneTest.clone();
        return tmp1;

    }
}


/**
 * @author CY
 */
public class StudentDemo {
    public static void main(String[] args) throws CloneNotSupportedException {
//        Student[] arr=new Student[]{
//                new Student("chenyong",21),
//                new Student("zhangyanfei",23),
//                new Student("yuanxiang",21),
//                new Student("meinv",15)
//        };
        CloneTest cloneTest = new CloneTest();
        Student chen = new Student("chen", 21, cloneTest);
        Student clone = (Student) chen.clone();


        System.out.println(clone.cloneTest.age);
        System.out.println(chen.cloneTest.age);
        System.out.println();
        clone.cloneTest.age = 15;
        System.out.println(clone.cloneTest.age);
        System.out.println(chen.cloneTest.age);


//        sort(arr);
//        System.out.println(Arrays.toString(arr));


    }

    public static void sort(Comparable[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = arr.length - 1; j > i; j--) {
                if (arr[j].compareTo(arr[j - 1]) < 0) {
                    Comparable tmp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = tmp;
                }
            }
        }
    }
}
